<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.dao.${className}Dao">
    <resultMap id="BaseResultMap" type="${classNameFirstLower}">
        <id column="id" property="id" jdbcType="VARCHAR"></id>
        <#list columns as column>
            <result column="${column.name}" property="${column.javaPropertyName}" jdbcType="${column.jdbcType}"></result>
        </#list>
    </resultMap>

    <resultMap id="GridResultMap" type="map">
        <id column="id" property="id" jdbcType="VARCHAR"></id>
        <#list columns as column>
            <result column="${column.name}" property="${column.javaPropertyName}" jdbcType="${column.jdbcType}"></result>
        </#list>
    </resultMap>

    <#noparse>
        <sql id="whereClase">
            <where>
                <foreach collection="creations" item="creation">
                    and ${creation.property} ${creation.operation} #{creation.value}
                </foreach>
            </where>
        </sql>
    </#noparse>

    <#noparse>
        <sql id="gridWhereClase">
            <where>
                <foreach collection="creations" item="creation">
                    and a.${creation.property} ${creation.operation} #{creation.value}
                </foreach>
            </where>
        </sql>
    </#noparse>


    <select id="get" resultMap="BaseResultMap">
        select * from ${databaseTableName} where id = <#noparse> #{id,jdbcType=VARCHAR} </#noparse>
    </select>

    <select id="search" resultMap="BaseResultMap">
        select * from ${databaseTableName}
        <include refid="whereClase"/>
    </select>

    <select id="grid" resultMap="GridResultMap">
        select
            a.id,
        <#list columns as column>
            <#if column.link?? && column.link!=''>
                <#if column.link?split(",")?size == 1>
                    ${(column_index + 2)?lower_abc}.name as ${column.name}<#if column_has_next>,</#if>
                </#if>
                <#if column.link?split(",")?size == 3>
                    ${(column_index + 2)?lower_abc}.${column.link?split(",")[2]} as ${column.name}<#if column_has_next>,</#if>
                </#if>
            <#else>
                a.${column.name} <#if column_has_next>,</#if>
            </#if>
        </#list>
            from ${databaseTableName} a
        <#list columns as column>
            <#if column.link?? && column.link!=''>
                <#if column.link?split(",")?size == 1>
                    left join sys_dic_item ${(column_index + 2)?lower_abc}
                    on a.${column.name} = ${(column_index + 2)?lower_abc} .code
                    and ${(column_index + 2)?lower_abc}.dic_id = (select id from sys_dic where code = '${column.link?split(",")[0]}')
                </#if>
                <#if column.link?split(",")?size == 3>
                    left join  ${column.link?split(",")[0]} ${(column_index + 2)?lower_abc}
                    on a.${column.name} = ${(column_index + 2)?lower_abc}.${column.link?split(",")[1]}
                </#if>
            </#if>
        </#list>
        <include refid="gridWhereClase"/>
    </select>

    <insert id="insert">
        insert into ${databaseTableName} (
            id,
            <#list columns as column>
                ${column.name}<#if column_has_next>,</#if>
            </#list>
        )
        values (
            <#noparse>#{id,jdbcType=VARCHAR},</#noparse>
        <#list columns as column>
            <#noparse>#{</#noparse>${column.javaPropertyName},jdbcType=${column.jdbcType}} <#if column_has_next>,</#if>
        </#list>
        )
    </insert>

    <insert id="batchInsert">
        insert into ${databaseTableName} (
        id,
            <#list columns as column>
                ${column.name}<#if column_has_next>,</#if>
            </#list>
        )
        values
        <foreach collection="list" item="entity" separator=",">
            (
                <#noparse>#{entity.id,jdbcType=VARCHAR},</#noparse>
            <#list columns as column>
                <#noparse>#{entity.</#noparse>${column.javaPropertyName},jdbcType=${column.jdbcType}} <#if column_has_next>,</#if>
            </#list>
            )
        </foreach>
    </insert>

    <update id="update">
        update ${databaseTableName}
        set
        <#list columns as column>
            ${column.name} = <#noparse>#{</#noparse>${column.javaPropertyName},jdbcType=${column.jdbcType}}<#if column_has_next>,</#if>
        </#list>
        where id = <#noparse> #{id,jdbcType=VARCHAR} </#noparse>
    </update>

    <delete id="delete">
        delete from ${databaseTableName} where id = <#noparse> #{id,jdbcType=VARCHAR} </#noparse>
    </delete>

    <delete id="deleteByCreations">
        delete from ${databaseTableName}
        <include refid="whereClase"/>
    </delete>

</mapper>